﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using RN.ERP.Entity;
using RN.ERP.Entity.ZBDLRY;
using System.Data.Entity;
using Fly.Log;
using RN.ERP.DAL.Context;
using Newtonsoft.Json;

namespace RN.ERP.DAL.LcDAL
{
    public class BasicDAL
    {
        ZBDL_Context dbContext = new ZBDL_Context();

        public IList<e_ScoreType_zbdl> GetEScoreType(int bKind, int bType, string Areacode, string condtion)
        {
            string sql = "select * from e_ScoreType where byearflag=4 and  bKind=@bKind and bType=@bType";

            if (!string.IsNullOrEmpty(condtion))
            {
                sql += condtion;
            }

            SqlParameter[] parameter ={
                new SqlParameter("@bKind",bKind),
                new SqlParameter("@bType",bType)
            };

            List<e_ScoreType_zbdl> list = dbContext.Database.SqlQuery<e_ScoreType_zbdl>(sql, parameter).ToList();
            return list;
        }

        public IList<e_Score_zbdl> GetEScore(int typeID, string condtion)
        {
            string sql = "select * from e_Score where 1=1";
            IList<SqlParameter> parameters = new List<SqlParameter>();

            if (typeID != -1)
            {
                sql += " and TypeID=@TypeID";
                parameters.Add(new SqlParameter("@TypeID", typeID));
            }
            if (!string.IsNullOrEmpty(condtion))
            {
                DTO.ScoreItem queryConditon = JsonConvert.DeserializeObject<DTO.ScoreItem>(condtion);
                if (queryConditon != null)
                {
                    if (queryConditon.AppID != null)
                    {
                        sql += " and AppID=@AppID";
                        parameters.Add(new SqlParameter("@AppID", queryConditon.AppID));
                    }
                    if (queryConditon.ScoreID != null)
                    {
                        sql += " and ID=@ID";
                        parameters.Add(new SqlParameter("@ID", queryConditon.ScoreID));
                    }
                }
            }

            IList<e_Score_zbdl> list = dbContext.Database.SqlQuery<e_Score_zbdl>(sql, parameters.ToArray()).ToList();
            return list;
        }
    }
}
